package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.databinding.FgWeightControlMethodBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch


class WeightControlMethodFg : BasePageFragment<FgWeightControlMethodBinding>(FgWeightControlMethodBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val activity = (requireActivity() as GuidePbAc)
        val data = activity.viewModel.userGuideState

        lifecycleScope.launch {
            data.collectLatest {
                when (it.weighControlMethod) {
                    0 -> {
                        binding.tvIntermittentFasting.isSelected = true
                        binding.tvCalorieControl.isSelected = false
                    }

                    1 -> {
                        binding.tvIntermittentFasting.isSelected = false
                        binding.tvCalorieControl.isSelected = true
                    }
                }
            }
        }

        binding.tvIntermittentFasting.setOnClickListener {
            data.update { it.copy(weighControlMethod = 0) }
        }
        binding.tvCalorieControl.setOnClickListener {
            data.update { it.copy(weighControlMethod = 1) }
        }
    }

}